package com.wechat.android.app.ui.login;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;

import com.wechat.android.app.R;
import com.wechat.android.app.db.UserHelper;
import com.wechat.android.app.global.Constants;
import com.wechat.android.app.ui.BaseActivity;
import com.wechat.android.app.utils.ActivityHelper;
import com.wechat.android.app.utils.MyLog;
import com.wechat.android.app.utils.PhoneUtils;
import com.wechat.android.app.widget.CustomFontTextView;
import com.wechat.android.app.widget.MyEditText;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by yang on 2018/1/18.
 * 修改手机
 */

public class UpdateUserPhoneActivity extends BaseActivity {
    @BindView(R.id.et_mobile)
    MyEditText etMobile;
    @BindView(R.id.et_sms_code)
    MyEditText etSmsCode;
    @BindView(R.id.tv_sms_code)
    CustomFontTextView tvSmsCode;
    @BindView(R.id.btn_commit)
    CustomFontTextView btnCommit;
    @BindView(R.id.tv_user_phone)
    CustomFontTextView tvUserPhone;
    @BindView(R.id.tv_code)
    CustomFontTextView tvCode;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_update_user_phone;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText("\uD81B\uDF1A\uD81B\uDF60\uD81B\uDF1A\uD81B\uDF51\uD81B\uDF61\uD81B\uDF3A\uD81B\uDFC9\uD81B\uDF2F\uD81B\uDFA9\uD81B\uDF1E\uD81B\uDF6A\uD81B\uDF21\uD81B\uDF51\uD81B\uDF79\uD81B\uDF3B\uD81B\uDFC4\uD81B\uDFC0");
        tvUserPhone.setText("\uD81B\uDF3A\uD81B\uDFC9\uD81B\uDF2F\uD81B\uDFA9\uD81B\uDF04\uD81B\uDF54\uD81B\uDF50\uD81B\uDF0A\uD81B\uDFA8");
        tvCode.setText("\uD81B\uDF04\uD81B\uDFD4\uD81B\uDF1E\uD81B\uDF6A\uD81B\uDF16\uD81B\uDF59");
        tvSmsCode.setText("\uD81B\uDF37\uD81B\uDF7A\uD81B\uDF3A\uD81B\uDFD4\uD81B\uDF04\uD81B\uDFD4\uD81B\uDF1E\uD81B\uDF6A\uD81B\uDF16\uD81B\uDF59");
        btnCommit.setText("\uD81B\uDF3A\uD81B\uDFBD\uD81B\uDF04\uD81B\uDF7B");
    }

    @Override
    protected void updateViews() {

    }


    @OnClick({R.id.tv_sms_code, R.id.btn_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_sms_code:
                String phone = etMobile.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    //手机号不能为空
                    mMStatusDialog.show("\uD81B\uDF3A\uD81B\uDFC9\uD81B\uDF2F\uD81B\uDFA9\uD81B\uDF0A\uD81B\uDF61\uD81B\uDF26\uD81B\uDFC4\uD81B\uDF3D\uD81B\uDFA0\uD81B\uDF1E\uD81B\uDF6A\uD81B\uDF26\uD81B\uDFC4\uD81B\uDF04\uD81B\uDFD6", null);
                    etMobile.requestFocus();
                    return;
                }
                if (!PhoneUtils.isMobileNO(phone)) {
                    //手机号格式错误
                    mMStatusDialog.show("\uD81B\uDF3A\uD81B\uDFC9\uD81B\uDF2F\uD81B\uDFA9\uD81B\uDF0A\uD81B\uDF61\uD81B\uDF3B\uD81B\uDFD8\uD81B\uDF3B\uD81B\uDF54\uD81B\uDF26\uD81B\uDFA9\uD81B\uDF3D\uD81B\uDFBE", null);
                    etMobile.requestFocus();
                    return;
                }
                getSmsCode(phone);
                break;
            case R.id.btn_commit:
                String userPhone = etMobile.getText().toString().trim();
                String smsCode = etSmsCode.getText().toString().trim();
                if (TextUtils.isEmpty(userPhone)) {
                    //手机号不可为空
                    mMStatusDialog.show("\uD81B\uDF3A\uD81B\uDFC9\uD81B\uDF2F\uD81B\uDFA9\uD81B\uDF0A\uD81B\uDF61\uD81B\uDF26\uD81B\uDFC4\uD81B\uDF3D\uD81B\uDFA0\uD81B\uDF1E\uD81B\uDF6A\uD81B\uDF26\uD81B\uDFC4\uD81B\uDF04\uD81B\uDFD6", null);
                    etMobile.requestFocus();
                    return;
                }
                if (!PhoneUtils.isMobileNO(userPhone)) {
                    //手机号格式错误
                    mMStatusDialog.show("\uD81B\uDF3A\uD81B\uDFC9\uD81B\uDF2F\uD81B\uDFA9\uD81B\uDF0A\uD81B\uDF61\uD81B\uDF3B\uD81B\uDFD8\uD81B\uDF3B\uD81B\uDF54\uD81B\uDF26\uD81B\uDFA9\uD81B\uDF3D\uD81B\uDFBE", null);
                    etMobile.requestFocus();
                    return;
                }

                if (TextUtils.isEmpty(smsCode)) {
                    //验证码不可为空
                    mMStatusDialog.show("\uD81B\uDF04\uD81B\uDFD4\uD81B\uDF1E\uD81B\uDF6A\uD81B\uDF16\uD81B\uDF59\uD81B\uDF26\uD81B\uDFC4\uD81B\uDF3D\uD81B\uDFA0\uD81B\uDF1E\uD81B\uDF6A\uD81B\uDF26\uD81B\uDFC4\uD81B\uDF04\uD81B\uDFD6", null);
                    etSmsCode.requestFocus();
                    return;
                }
                updateBindPhone(userPhone, smsCode);
                break;
        }
    }


    //修改绑定账号
    private void updateBindPhone(final String userPhone, String smsCode) {
        mMProgressDialog.showNoText();
        HttpParams params = new HttpParams();
        params.put("phone", userPhone);
        params.put("code", smsCode);
        params.put("userId", UserHelper.getInstance().getUserInfoModel(UpdateUserPhoneActivity.this).getUserId() + "");
        EasyHttp.post(Constants.API_UPDATE)
                .params(params)
                .timeStamp(true)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mMProgressDialog.dismiss();
                    }

                    @Override
                    public void onError(ApiException e) {
                        MyLog.e("yang", e.toString());
                        mMStatusDialog.show("\uD81B\uDF1E\uD81B\uDFA9\uD81B\uDF04\uD81B\uDF7B\uD81B\uDF2F\uD81B\uDF51\uD81B\uDF62\uD81B\uDF3D\uD81B\uDF54\uD81B\uDF16\uD81B\uDF60", null);
                    }

                    @Override
                    public void onSuccess(String data) {
                        MyLog.e("yang", "修改手机成功==" + data);
                        String mData = PhoneUtils.checkResponseFlag(UpdateUserPhoneActivity.this, data);
                        if (mData != null) {
                            ActivityHelper.getInstance().finishAllActivity();
                            startActivity(new Intent(UpdateUserPhoneActivity.this, LoginActivity.class));
                        }

                    }
                });
    }


    //获取短信验证码接口
    private void getSmsCode(String phone) {
        mMProgressDialog.showNoText();
        HttpParams params = new HttpParams();
        params.put("phone", phone);
        EasyHttp.post(Constants.API_REQSMSCODE)
                .params(params)
                .timeStamp(true)//添加时间戳
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mMProgressDialog.dismiss();
                    }

                    @Override
                    public void onError(ApiException e) {
                        MyLog.e("yang", "onError==" + e.toString());
                        mMStatusDialog.show("\uD81B\uDF1E\uD81B\uDFA9\uD81B\uDF04\uD81B\uDF7B\uD81B\uDF2F\uD81B\uDF51\uD81B\uDF62\uD81B\uDF3D\uD81B\uDF54\uD81B\uDF16\uD81B\uDF60", null);
                    }

                    @Override
                    public void onSuccess(String data) {
                        MyLog.e("yang", "onSuccess==" + data);
                        String mData = PhoneUtils.checkResponseFlag(UpdateUserPhoneActivity.this, data);
                        if (mData != null) {
                            timer.start();
                        }


                    }
                });

    }


    //   倒计时响应事件  timer.start();
    private CountDownTimer timer = new CountDownTimer(60000, 1000) {

        @Override
        public void onTick(long millisUntilFinished) {
            tvSmsCode.setClickable(false);// 设置不能点击
            tvSmsCode.setText(millisUntilFinished / 1000 + "\uD81B\uDF37\uD81B\uDF77\uD81B\uDF23\uD81B\uDF54\uD81B\uDF0E\uD81B\uDF7A\uD81B\uDF3A\uD81B\uDFBD\uD81B\uDF43\uD81B\uDFC4\uD81B\uDF50\uD81B\uDF2F\uD81B\uDF61");// 设置倒计时时间
            tvSmsCode.setTextColor(getResources().getColor(R.color.mainTabFontColor)); // 设置为灰色，这时是不能点击的
        }

        @Override
        public void onFinish() {
            tvSmsCode.setText("\uD81B\uDF37\uD81B\uDF7A\uD81B\uDF3A\uD81B\uDFD4\uD81B\uDF04\uD81B\uDFD4\uD81B\uDF1E\uD81B\uDF6A\uD81B\uDF16\uD81B\uDF59");
            tvSmsCode.setClickable(true);// 重新获得点击
            tvSmsCode.setTextColor(getResources().getColor(R.color.appThemeColor));

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
